/**
 *   \file list_active_domains.c
 *   \brief gcc -Wall -o list_active_domains list_active_domains.c -lvirt
 *
 *  LavenLiu
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>

int main(int argc, char *argv[])
{
    virConnectPtr  conn;
    int            i;
    int            num_domains;
    int           *active_domains;

    conn = virConnectOpen("qemu:///system");
    if (conn == NULL) {
		fprintf(stderr, "Failed to connect to qemu:///system\n");
		return 1;
    }
    
    num_domains = virConnectNumOfDomains(conn);

    active_domains = malloc(sizeof(int) * num_domains);
    num_domains = virConnectListDomains(conn, active_domains, num_domains);

    printf("Active domain IDs:\n");
    for (i = 0; i < num_domains; i++) {
		printf("  %d\n", active_domains[i]);
    }
    free(active_domains);

    return 0;
}
